package net.aiion.weave.impl.xml.events;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import net.aiion.weave.spi.xml.XmlEventFactory;
import net.aiion.weave.spi.xml.XmlQualifiedName;
import net.aiion.weave.spi.xml.events.AttributeXmlEvent;
import net.aiion.weave.spi.xml.events.ElementStartXmlEvent;
import net.aiion.weave.spi.xml.events.NamespaceXmlEvent;

public class ElementStartXmlEventImpl extends XmlEventImpl implements ElementStartXmlEvent
{
    private final XmlQualifiedName xmlQualifiedName;
    private final Collection<AttributeXmlEvent> xmlAttributes;
    private final Collection<NamespaceXmlEvent> xmlNamespaces;

    public ElementStartXmlEventImpl(final XmlQualifiedName anXmlQualifiedName,
        final Collection<AttributeXmlEvent> anXmlAttributes, final Collection<NamespaceXmlEvent> anXmlNamespaces)
    {
        xmlQualifiedName = anXmlQualifiedName;
        if (anXmlAttributes == null) {
            xmlAttributes = new ArrayList<>();
        } else {
            xmlAttributes = anXmlAttributes;
        }

        if (anXmlNamespaces == null) {
            xmlNamespaces = new ArrayList<>();
        } else {
            xmlNamespaces = anXmlNamespaces;
        }
    }

    @Override
    public int getType() {
        return START_ELEMENT;
    }

    @Override
    public XmlQualifiedName getName() {
        return xmlQualifiedName;
    }

    @Override
    public Collection<AttributeXmlEvent> getAttributes() {
        return Collections.unmodifiableCollection(xmlAttributes);
    }

    @Override
    public AttributeXmlEvent getAttribute(final XmlQualifiedName anXmlQualifiedName) {
        for (AttributeXmlEvent xmlAttribute : xmlAttributes) {
            if (anXmlQualifiedName.equals(xmlAttribute.getName())) {
                return xmlAttribute;
            }
        }

        return null;
    }

    @Override
    public String getAttributeValue(final XmlQualifiedName aXmlQualifiedName) {
        return getAttribute(aXmlQualifiedName).getValue();
    }

    @Override
    public AttributeXmlEvent getAttribute(final String aLocalName) {
        return getAttribute(XmlEventFactory.getInstance().createQualifiedName(aLocalName));
    }

    @Override
    public String getAttributeValue(final String aLocalName) {
        final AttributeXmlEvent attributeXmlEvent = getAttribute(aLocalName);
        if (attributeXmlEvent == null) {
            return null;
        }

        return attributeXmlEvent.getValue();
    }

    @Override
    public Collection<NamespaceXmlEvent> getNamespaces() {
        return Collections.unmodifiableCollection(xmlNamespaces);
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("XmlStartElementImpl[xmlQualifiedName=");
        builder.append(xmlQualifiedName);
        builder.append(", xmlAttributes=");
        builder.append(xmlAttributes);
        builder.append(", xmlNamespaces=");
        builder.append(xmlNamespaces);
        builder.append("]");
        return builder.toString();
    }
}
